﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;

namespace Baosight.ZNH.ZM.UI
{
    public partial class ZM01 :EFView
    {
        public ZM01()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ZM01_Loaded);
        }
        private void ZM01_Loaded(object sender, RoutedEventArgs e)
        {
            this.codeGrid.RowDoubleClick -= new EventHandler<RowDoubleClickEventArgs>(codeGrid_RowDoubleClick);
            this.codeGrid.RowDoubleClick += new EventHandler<RowDoubleClickEventArgs>(codeGrid_RowDoubleClick);
        }

        /// <summary>
        /// 重新初始化方法
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);

            //查询条件的设置
            if ((initData.GetAttributes().ContainsKey("queryConditions")))
            {
                string queryConditions = initData.Get("queryConditions").ToString();
                string[] conditions = queryConditions.Split('|');
                if (conditions.Length < 4)
                {
                    this.query.Visibility = Visibility.Visible;
                    List<EFDataField> fields = this.qureyArea.GetChildObjects<EFDataField>(null, true);
                    int i = 1;
                    foreach(EFDataField field in fields)
                    {
                        string name = field.Name;
                        if (i <= conditions.Length)
                        {
                            if (name.Equals("field" + i.ToString()))
                            {
                                field.eBindingName = "inqu_status-0-" + conditions[i - 1];
                                field.Visibility = Visibility.Visible;
                                //如果从initLoad方法中返回了元数据，取得查询条件列的中文名
                                if (initData.Blocks.ContainsKey("result"))
                                {
                                    var block = initData.Blocks["result"];
                                    if (block.Columns.ContainsKey(conditions[i - 1]))
                                    {
                                        field.eLabel = block.Columns[conditions[i - 1]].Desc;
                                    }
                                }
                                i++;                                                       
                            }
                        }
                        else
                        {
                            field.Visibility = Visibility.Collapsed;
                        }
                    }
                }
            }

            //EFGrid中添加指定的列
            string columns = initData.Get("columns").ToString();
            string[] cols = columns.Split('|');
            if (initData.Blocks.ContainsKey("result"))
            {
                var block = initData.Blocks["result"];
                for (int i = 0; i < cols.Length; i++)
                {
                    if (block.Columns.ContainsKey(cols[i]))
                    {
                        EFColumn item = new EFColumn();
                        item.eHeader = block.Columns[cols[i]].Desc;
                        item.eColumnName = block.Columns[cols[i]].Name;
                        item.eName = block.Columns[cols[i]].Name;                           
                        this.codeGrid.Columns.Add(item);

                        if (cols[i].IndexOf("Id") > 0)
                        {
                            item.IsVisible = false;
                        }
                    }
                }
            }

            //设置服务名称和方法名
            string bindingService = initData.Get("bindingService") as string;
            string[] tokens = bindingService.Split('.');
            if (tokens.Length == 2)
            {
                this.eServiceName = tokens[0];
                this.eMethodName = tokens[1];

                this.codeGrid.eServiceName = tokens[0];
                this.codeGrid.eMethodName = tokens[1];
            }


            //初始条件的设置
            if ((initData.GetAttributes().ContainsKey("initParam")))
            {
                string initParam = initData.Get("initParam").ToString();
                string[] Params = initParam.Split('|');
                if (Params.Length < 4)
                {
                    this.query.Visibility = Visibility.Visible;
                    List<EFDataField> fields = this.paramArea.GetChildObjects<EFDataField>(null, true);
                    int i = 1;
                    foreach (EFDataField field in fields)
                    {
                        string name = field.Name;
                        if (i <= Params.Length)
                        {
                            if (name.Equals("Param" + i.ToString()))
                            {
                                string[] value = Params[i - 1].Split(':');
                                if (value.Length >= 2)
                                {
                                    field.eBindingName = "inqu_status-0-" + value[0];
                                    field.Value = value[1];
                                }
                                i++;
                            }
                        }
                    }
                }
            }
            
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void query_Click(object sender, RoutedEventArgs e)
        {
            base.OnQueryButtonClick(sender, e);
        }

        /// <summary>
        /// 双击数据事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void codeGrid_RowDoubleClick(object sender, RowDoubleClickEventArgs e)
        {
            NotifyViewDataChanged(e.DataItem);
        }

        /// <summary>
        /// 清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clean_Click(object sender, RoutedEventArgs e)
        {
            NotifyViewDataChanged("");
        }

    }
}
